﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using ChangDiTools.Tool;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ChangDiTools.Demo {
    public class DimStyles {
        [CommandMethod("NewDim")]
        public void NewDim() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            string dimName = "";
            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                while (dimName=="")
                {
                    PromptResult pr = ed.GetString("\n请输入标注样式名称");
                    if(pr.Status!=PromptStatus.OK) return;
                    try
                    {
                        //验证输入的标注样式是否合法
                        SymbolUtilityServices.ValidateSymbolName(pr.StringResult,false);
                        dimName = pr.StringResult;
                        ObjectId dimId = db.AddDimStyle(dimName);
                        DimStyleTableRecord dstr=tr.GetObject(dimId,OpenMode.ForWrite) as DimStyleTableRecord;
                        dstr.Dimasz = 3;//箭头大小
                        dstr.Dimexe = 3;//超出尺寸线的大小
                        dstr.Dimtad = 1;//文字位于标注线的上方
                        dstr.Dimtxt = 3;//标注文字的高度
                        //设置新添加的标注样式为当前标注样式
                        db.Dimstyle = dimId;
                        //为方式出现替代样式问题，请添加下面的语句
                        db.SetDimstyleData(dstr);
                        break;//添加标注样式成功，跳出循环
                    }
                    catch
                    {
                        ed.WriteMessage("\n符号表名称不合法\n");
                    }
                }
                tr.Commit();
            }
        }
        [CommandMethod("CopyDim")]
        public static void CopyDim() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo=new PromptEntityOptions("请选择标注对象");
            peo.SetRejectMessage("你选择的不是标注对象");
            peo.AddAllowedClass(typeof(Dimension),false);
            PromptEntityResult per = ed.GetEntity(peo);
            if(per.Status!=PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                Dimension dim=tr.GetObject(per.ObjectId,OpenMode.ForRead) as Dimension;
                DimStyleTableRecord dstr1=new DimStyleTableRecord();
                dstr1.CopyFrom(dim.GetDimstyleData());
                dstr1.Name = "标注对象样式";
                DimStyleTableRecord dstr2=new DimStyleTableRecord();
                dstr2.CopyFrom(db.GetDimstyleData());
                dstr2.Name = "数据库样式";
                DimStyleTableRecord dstr3=new DimStyleTableRecord();
                dstr3.CopyFrom(dstr2);
                dstr3.Name = "复制样式";
                DimStyleTable dst=tr.GetObject(db.DimStyleTableId,OpenMode.ForWrite) as DimStyleTable;
                dst.Add(dstr1);
                dst.Add(dstr2);
                dst.Add(dstr3);
                tr.AddNewlyCreatedDBObject(dstr1,true);
                tr.AddNewlyCreatedDBObject(dstr2,true);
                tr.AddNewlyCreatedDBObject(dstr3,true);
                tr.Commit();
            }
        }
    }
}
